package wms

import (
	"context"
	"encoding/json"
	"ez/apps/wms/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type InLog struct {
	mgo.BaseDoc  `bson:"-" json:"-"`
	Id           int64       `json:"id" bson:"id,omitempty"`
	Sn           string      `json:"sn" bson:"sn"`                     //单号
	Item         *ItemSku    `json:"item" bson:"item"`                 //物品
	ItemId       int64       `json:"itemId" bson:"itemId"`             //物品
	Type         *IOType     `json:"type" bson:"type"`                 //入库类型
	TypeId       int64       `json:"typeId" bson:"typeId"`             //入库类型
	Department   *Department `json:"department" bson:"department"`     //入库部门
	DepartmentId int64       `json:"departmentId" bson:"departmentId"` //入库部门
	IsCanceled   bool        `json:"isCanceled" bson:"isCanceled"`     //是否冲红
	NumIn        float64     `json:"numIn" bson:"numIn"`               //入库数量
	NumInReal    float64     `json:"numInReal" bson:"numInReal"`       //入库数量
	NumOut       float64     `json:"numOut" bson:"numOut"`             //出库数量
	NumLeft      float64     `json:"numLeft" bson:"numLeft"`           //剩余数量
	BatchCode    string      `json:"batchCode" bson:"batchCode"`       //批次号
	BatchPrice   float64     `json:"batchPrice" bson:"batchPrice"`     //批次价格
	Cost         float64     `json:"cost" bson:"cost"`                 //总价
	CostReal     float64     `json:"costReal" bson:"costReal"`         //总价
	HeapCode     string      `json:"heapCode" bson:"heapCode"`         //堆号
	Pos          string      `json:"pos" bson:"pos"`                   //库位号
	Supplier     string      `json:"supplier" bson:"supplier"`         //供应商
	Brand        string      `json:"brand" bson:"brand"`               //品牌
	Link         string      `json:"link" bson:"link"`                 //Link
	LinkTo       string      `json:"linkTo" bson:"linkTo"`             //LinkTo
	Note         string      `json:"note" bson:"note"`                 //备注
	CreateBy     int64       `json:"createBy" bson:"createBy"`         //创建人ID
	CreateName   string      `json:"createName" bson:"createName"`     //创建人
	CreateAt     time.Time   `json:"createAt" bson:"createAt"`
	UpdateAt     time.Time   `json:"updateAt" bson:"updateAt"`
	DeleteAt     *time.Time  `json:"deleteAt" bson:"deleteAt"`
}

func (this *InLog) DocName() string { return "InLog" }
func (this *InLog) GetId() int64    { return this.Id }
func (this *InLog) SetId(id int64)  { this.Id = id }
func (this *InLog) Create() error {
	return this.GetFactory().Create(this)
}
func (this *InLog) Replace() error {
	return this.GetFactory().Replace(this)
}
func (this *InLog) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *InLog) Delete() error {
	return this.GetFactory().Delete(this)
}
func (this *InLog) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *InLog) Destroy() error {
	return this.GetFactory().Destroy(this)
}
func (this *InLog) ToString() string {
	return string(this.ToBytes())
}
func (this *InLog) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *InLog) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *InLog) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}
func (this *InLog) LoadItem() {
	if this.ItemId == 0 {
		return
	}
	this.Item, _ = NewItemSkuCrud().FindId(this.ItemId)
}
func (this *InLog) LoadType() {
	if this.TypeId == 0 {
		return
	}
	this.Type, _ = NewIOTypeCrud().FindId(this.TypeId)
}
func (this *InLog) LoadDepartment() {
	if this.DepartmentId == 0 {
		return
	}
	this.Department, _ = NewDepartmentCrud().FindId(this.DepartmentId)
}

func (this *InLog) ClearRelationsBeforeSave() mgo.Doc {
	this.Item = nil
	this.Type = nil
	this.Department = nil
	return this
}
func neverUsed_InLog() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type InLogAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *InLog
	Session *ez.Session
}

func NewInLogAccessControl(model *InLog, action string, session *ez.Session) *InLogAccessControl {
	ctrl := &InLogAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.InLogAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
